﻿using Autofac;
using Common;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DBContext;
using EV.EventBus;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ZoneLine.Common;
using ZoneLine.Models;
using ZoneLine.Models.PrintModeConfig;
using ZoneLine.Models.StationConfig;
using ZoneLine.Printers;
using ZoneLine.Stations;
using ZoneLine.Stations.XWStation;

namespace ZoneLine.ViewModels
{
    public class XWOpMainViewModel : ObservableObject
    {
        private IModelDAL _dal;
        public XWOpMainViewModel(IModelDAL dal, XW010SStation xw010SStation, XW010LStation xw010LStation, XW010RStation xw010RStation)
        {
            _dal = dal;

            InitStationInfo();

            var xw010SConfig = _dal.GetModel<XW010SStationConfig>(p => true);
            if (xw010SConfig == null)
            {
                xw010SConfig = new XW010SStationConfig();
                _dal.AddModel(xw010SConfig);
            }
            Xw010SStation = xw010SStation;
            Xw010SStation.Init(xw010SConfig.Ip, 102, xw010SConfig.ReadStartAddr, xw010SConfig.EndUploadCodeAddr, xw010SConfig.EndUploadDataAddr, xw010SConfig.AllowWorkAddr);

            var xw010LConfig = _dal.GetModel<XW010LStationConfig>(p => true);
            if (xw010LConfig == null)
            {
                xw010LConfig = new XW010LStationConfig();
                _dal.AddModel(xw010LConfig);
            }
            Xw010LStation = xw010LStation;
            Xw010LStation.Init(xw010LConfig.Ip, 102, xw010LConfig.ReadStartAddr, xw010LConfig.EndUploadCodeAddr, xw010LConfig.EndUploadDataAddr, xw010LConfig.AllowWorkAddr);

            var xw010RConfig = _dal.GetModel<XW010RStationConfig>(p => true);
            if (xw010RConfig == null)
            {
                xw010RConfig = new XW010RStationConfig();
                _dal.AddModel(xw010RConfig);
            }
            Xw010RStation = xw010RStation;
            Xw010RStation.Init(xw010RConfig.Ip, 102, xw010RConfig.ReadStartAddr, xw010RConfig.EndUploadCodeAddr, xw010RConfig.EndUploadDataAddr, xw010RConfig.AllowWorkAddr);



            Task.Run(() =>
            {
                Xw010SStation.Start();
            });

            Task.Run(() =>
            {
                Xw010LStation.Start();
            });

            Task.Run(() =>
            {
                Xw010RStation.Start();
            });
        }

        private void InitStationInfo()
        {
            var config = XWStationEnableConfig.GetPara();
            if (config.XWOP010SStationEnable == 1)
            {
                Cache.WorkStationValidationList.Add(nameof(XW010SStation));
            }
            if (config.XWOP010LStationEnable == 1)
            {
                Cache.WorkStationValidationList.Add(nameof(XW010LStation));
            }
            if (config.XWOP010RStationEnable == 1)
            {
                Cache.WorkStationValidationList.Add(nameof(XW010RStation));
            }
        }

        private XW010SStation _xw010SStation;

        public XW010SStation Xw010SStation
        {
            get { return _xw010SStation; }
            set => SetProperty(ref _xw010SStation, value);
        }

        private XW010LStation _xw010LStation;

        public XW010LStation Xw010LStation
        {
            get { return _xw010LStation; }
            set => SetProperty(ref _xw010LStation, value);
        }

        private XW010RStation _xw010RStation;

        public XW010RStation Xw010RStation
        {
            get { return _xw010RStation; }
            set => SetProperty(ref _xw010RStation, value);
        }
    }
}
